package com.freewater.javabase.demo.network.post;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;

public class PostDataDemo {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://apitest.hitcxedu.com/login/loginChk");
            FormPoster formPoster = new FormPoster(url);
            // loginName=admin&userPwd=hitcx@123456&captcha=1111
            formPoster.addParam("loginName", "admin");
            formPoster.addParam("userPwd", "hitcx@123456");
            formPoster.addParam("captcha", "1111");
            System.out.println("url: " + url);
            System.out.println("params:" + formPoster.getQueryString().toString());
            try (final InputStream inputStream = formPoster.post();
                 final BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
                final ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] bytes = new byte[1024];
                int count;
                while ((count = bufferedInputStream.read(bytes)) != -1) {
                    bos.write(bytes, 0, count);
                }
                System.out.println("response: " + bos.toString(Charset.forName("utf-8")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
